---
layout: base
title: Migration
---

<header class="jumbotron subhead" id="overview">
  <div class="container">
    <h3>Migrating database from previous version</h3>
  </div>
</header>

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <div id="sidebar" role="complementary" data-spy="affix" data-offset-top="80">
        <ul class="nav sidebar">
          <li class="active"><a href="#overview">Overview</a></li>
          <li><a href="#script">Sample Script</a></li>
        </ul>
      </div>
    </div>
    <div class="col-md-9">

        <section id="overview">
          <div class="page-header">
            <h1>1. Overview</h1>
          </div>
          <p class="lead">Most apps go through database schema changes as they grow and release multiple revisions. Sugar provides an easy way to upgrade the database. The inspiration is again from the way Rails migrations work. Here's how you'd do it:</p>

          <ul>
            <li><p class="lead">Create a folder named <code>sugar_upgrades</code> in your <code>assets</code> folder.</p></li>
            <li>
              <p class="lead">Create a file named <code>&lt;version&gt;.sql</code> in that folder, which corresponds to the database version. eg. <code>1.sql</code>, <code>2.sql</code> This file would contain all the update/alter queries for that particular version.</p>
            </li>
            <li>
              <p class="lead">Change the <CODE>VERSION</CODE> metadata field in AndroidManifest.xml to the appropriate version.</p>
              <div class="bs-docs-example">
{% highlight xml %}
<application android:label="@string/app_name" android:icon="@drawable/icon"
android:name="com.orm.SugarApp">
  .
  .
  <meta-data android:name="DATABASE" android:value="sugar_example.db" />
  <meta-data android:name="VERSION" android:value="2" />
  .
  .
</application>
{% endhighlight %}
              </div>

            </li>
            <li>
              <p class="lead">Sugar takes care of upgrading the database from its present version to the upgraded version. For eg: if the database is currently at version <code>1</code> and the upgraded version is <code>4</code>, it'd look for <code>2.sql</code>, <code>3.sql</code> and <code>4.sql</code> and execute all of the present files in that order.</p>
            </li>
          </ul>
        </section>

        <section id="script">
          <div class="page-header">
            <h1>2. Upgrade Script sample.</h1>
          </div>
          <p class="lead">This is a normal sql script file. You can add all your alter and insert/update queries, one line at a time. Each line is terminated by a <code>;</code> (semicolon).</p>

          <div class="bs-docs-example">
            2.sql

{% highlight sql %}
alter table NOTE add NAME TEXT;
{% endhighlight %}
          </div>
        </section>
    </div>
  </div>
</div>
